<template>
  <el-drawer
    @close="close"
    :visible.sync="dialogVisible"
    :title="title"
    :size="500"
    :wrapperClosable="false"
  >
    <el-row :gutter="10" class="box-card">
      <el-form ref="elForm" :model="formData" :rules="rules" size="small" label-width="100px">
        <el-col :span="24">
          <el-form-item label="音频标题" prop="name">
            <el-input v-model="formData.name" placeholder="请输入音频标题" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="音频上传" prop="fileUrl">
            <el-upload
              :action="upload.uploadVideo"
              :on-success="handleAvatarSuccess"
              :headers="upload.uploadHeader"
              accept="audio/*"
              :limit="1"
              :data="{fileSize: 10000}"
              :file-list="fileList"
              :on-remove="handleRemove"
            >
              <el-button size="small" type="primary" :disabled="isShowBtn">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="预览音频" prop="fileUrl">
            <audio :src="formData.fileUrl | getImgUrl" controls ref="audio"></audio>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div class="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { saveELearn } from '@/api/elearn'
import { getToken } from '@/utils/auth'
export default {
  props: [],
  data () {
    return {
      dialogVisible: false,
      videoDialogVisible: false,
      formData: {
        name: '', // 音频标题
        fileUrl: '', // 音频地址
        fileType: 'redBook'
      },
      fileList: [],
      isShowBtn: false,
      upload: {
        uploadVideo: process.env.VUE_APP_BASE_API + '/file/uploadFile',
        uploadHeader: {
          'jwt-token': getToken()
        }
      },
      rules: {
        name: [{
          required: true,
          message: '请输入音频标题',
          trigger: 'blur'
        }],
        fileUrl: [{
          required: true,
          message: '请上传音频',
          trigger: ['change', 'blur']
        }]
      },
      title: '新增音频',
      id: null,
      isEdit: false
    }
  },
  methods: {
    /**
     *
     * @param {*} isEdit 是否编辑
     * @param {*} data 编辑时需要的数据
     */
    async isShow (data, isEdit = false) {
      this.dialogVisible = true
      this.title = isEdit ? '编辑音频' : '新增音频'
      this.isEdit = isEdit
      await this.$nextTick()
      if (isEdit) {
        const formData = this.formData
        for (const key in formData) {
          formData[key] = data[key]
        }
        this.fileList.push({ name: data.name, url: data.fileUrl })
        this.isShowBtn = true
        this.id = data.id
      } else {
        this.formData.hdType = data
      }
    },
    handleAvatarSuccess (file) {
      if (file.code === 200) {
        this.formData.fileUrl = file.data.url
        this.isShowBtn = true
      } else {
        this.$notify.error(file.message)
      }
    },
    // 移除文件
    handleRemove () {
      this.formData.fileUrl = ''
      this.fileList = []
      this.isShowBtn = false
    },
    close () {
      this.fileList = []
      this.$refs.audio.pause()
      this.$refs.elForm.resetFields()
      this.dialogVisible = false
    },
    handelConfirm () {
      this.$refs.elForm.validate(async valid => {
        if (!valid) return
        const duration = this.$refs.audio?.duration?.toFixed(0)
        this.isEdit ? (await saveELearn({ ...this.formData, id: this.id, duration })) : (await saveELearn({ ...this.formData, duration }))
        this.$notify.success({
          title: `完成${this.title}`
        })
        this.$emit('update:visible')
        this.close()
      })
    }
  }
}

</script>
<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
  padding-right: 20px;
  width: 100%;
  padding-left: 10px;
}
.footer {
  display: flex;
  justify-content: space-around;
  margin-bottom: 40px;
  padding-left: 110px;
  padding-right: 20px;
  .el-button {
    width: 100%;
  }
}
</style>
